﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Office.OneNote.XmlViewer.Controls
{
    public class LazyTreeView : TreeView
    {
        protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
        {
            LazyTreeNode lazyNode = (e.Node as LazyTreeNode);

            if (lazyNode != null)
            {
                if (!lazyNode.IsLoaded)
                {
                    BeginUpdate();

                    lazyNode.IsLoaded = true;
                    lazyNode.Nodes.Clear();
                    lazyNode.OnBeforeFirstExpand();

                    EndUpdate();
                }
            }

            base.OnBeforeExpand(e);
        }
    }

    public class LazyTreeNode : TreeNode
    {
        private bool _isLoaded;

        public LazyTreeNode()
        {
            Nodes.Add(new DummyNode());
        }

        public bool IsLoaded
        {
            get { return _isLoaded; }
            set { _isLoaded = value; }
        }

        public virtual void OnBeforeFirstExpand()
        {
        }

        private sealed class DummyNode : TreeNode
        {
        }
    }
}
